package com.jrx.anytxn.param.service.partner.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.partner.PmPartnerInfoRep;
import com.jrx.anytxn.param.dto.partner.PmPartnerInfoRes;
import com.jrx.anytxn.param.entity.PmPartnerInfo;
import com.jrx.anytxn.param.mapper.ext.ExtPmPartnerInfoMapper;
import com.jrx.anytxn.param.service.partner.IPmPartnerInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@CacheConfig(cacheNames = "PARTNER_INFO")
public class PmPartnerInfoServiceImpl implements IPmPartnerInfoService {

    private static final Logger logger = LoggerFactory.getLogger(PmPartnerInfoServiceImpl.class);

    @Autowired
    private ISegmentService segmentService;
    @Resource
    private ExtPmPartnerInfoMapper extPmPartnerInfoMapper;


    @Override
    public PmPartnerInfoRes getByTenIdIdAndPartnerId(String tenantId, String partnerId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(partnerId)) {
            logger.error("合作伙伴ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PmPartnerInfo pmPartnerInfo = extPmPartnerInfoMapper.selectByTenantIdAndPartnerId(tenantId,partnerId);
        return BeanMapping.copy(pmPartnerInfo, PmPartnerInfoRes.class);
    }

    @Override
    public PmPartnerInfoRes addPartnerInfo(PmPartnerInfoRep pmPartnerInfoRep) throws TxnException {
        PmPartnerInfo table = extPmPartnerInfoMapper.selectByTemplateIdAndOrgIdAndTenantIdAndTableStatus(pmPartnerInfoRep.getPartnerId(),pmPartnerInfoRep.getOrganizationId(),pmPartnerInfoRep.getTenantId(),null);
        if (table != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PmPartnerInfo pmPartnerInfo = BeanMapping.copy(pmPartnerInfoRep, PmPartnerInfo.class);
        try {
            pmPartnerInfo.setId(segmentService.getId(Constant.PM_PARTNER_INFO));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        pmPartnerInfo.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPmPartnerInfoMapper.insertSelective(pmPartnerInfo);
        return BeanMapping.copy(pmPartnerInfo, PmPartnerInfoRes.class);
    }

    @Override
    public TxnPage<PmPartnerInfoRes> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PmPartnerInfoRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PmPartnerInfo> tableList = extPmPartnerInfoMapper.selectByTenantId(tenantId,null);
        if (tableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PmPartnerInfoRes> res = BeanMapping.copyList(tableList, PmPartnerInfoRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public List<PmPartnerInfoRes> findAllByTenId(String tenantId) {
        List<PmPartnerInfo> pmPartnerInfoList = extPmPartnerInfoMapper.selectByTenantId(tenantId,"1");
        return BeanMapping.copyList(pmPartnerInfoList, PmPartnerInfoRes.class);
    }

    @CacheEvict(key="'partner_'+#pmPartnerInfoRep.tenantId+'_'+#pmPartnerInfoRep.partnerId")
    @Override
    public PmPartnerInfoRes modifyPmPartnerInfo(PmPartnerInfoRep pmPartnerInfoRep) throws TxnBizException {
        PmPartnerInfo table = extPmPartnerInfoMapper.selectByTenantIdAndPartnerId(pmPartnerInfoRep.getTenantId(),pmPartnerInfoRep.getPartnerId());
        if (table == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        PmPartnerInfo pmPartnerInfo = BeanMapping.copy(pmPartnerInfoRep, PmPartnerInfo.class);
        // 更新
        extPmPartnerInfoMapper.updateByTenantIdAndPartnerId(pmPartnerInfo);
        return BeanMapping.copy(pmPartnerInfo, PmPartnerInfoRes.class);
    }

    @CacheEvict(key="'partner_'+#tenantId+'_'+#partnerId")
    @Override
    public Boolean removeByTenantIdAndPartnerId(String tenantId, String partnerId) throws TxnBizException {
        if (org.apache.commons.lang.StringUtils.isBlank(tenantId) || org.apache.commons.lang.StringUtils.isBlank(partnerId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPmPartnerInfoMapper.deleteByTenantIdAndPartnerId(tenantId, partnerId);
        return i > 0;
    }

    @Cacheable(key="'partner_'+#tenantId+'_'+#partnerId")
    @Override
    public PmPartnerInfo getByPartnerId(String tenantId, String partnerId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(partnerId)) {
            logger.error("合作伙伴ID或租户ID为空");
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PmPartnerInfo pmPartnerInfo = extPmPartnerInfoMapper.selectByTenantIdAndPartnerId(tenantId,partnerId);
        return pmPartnerInfo;
    }
}
